import os
from flask import Blueprint, flash, jsonify, redirect,render_template, request, url_for
from app.models.base import db
from app.models.experiment import Experiment
from app.models.request import Request

experimentBP = Blueprint('experiment',__name__)

@experimentBP.route('/<string:t_name>/<int:t_id>', methods=['GET','POST'])
def save_experiment(t_name,t_id):
    if request.method == 'GET':
        return render_template('experiment1.html',name=t_name,id=t_id)
    else:
        # 处理表单数据
        T_id = t_id
        course_number = request.form.get('course_number')
        question_text = request.form.get('question_text')
        question_image = request.files.get('question_image')
        answer_image = request.files.get('answer_image')
        LLMname = request.form.get('LLMname')
        score = request.form.get('score')
        comments = request.form.get('comments')
        print(score,T_id,course_number,question_text,comments,question_image)
        # 保存图片文件路径
        if question_image:
            question_image_path = os.path.join('C:\\Users\\ASUS\\Downloads\\project_v1_36094\\project_v1\\app\\static\\ansimg', question_image.filename)
            
            question_image.save(question_image_path)
        else:
            question_image_path = None

        if not question_text and not question_image_path:
            return jsonify({'message': 'Question text and question image cannot both be empty'}), 400

        answer_image_path = None

        if answer_image:
            answer_image_path = os.path.join('C:\\Users\\ASUS\\Downloads\\project_v1_36094\\project_v1\\app\\static\\ansimg', answer_image.filename)
            answer_image.save(answer_image_path)



        # 创建实验实例
        experiment = Experiment(
            T_id,
            course_number,
            question_text,
            question_image_path,
            answer_image_path,
            LLMname,
            int(score),
            comments
        )
        with db.auto_commit():
        # 添加到数据库会话并提交
            db.session.add(experiment)


        # 返回响应
        response = {
            'message': 'Experiment saved successfully',
            'id': experiment.E_id
        }

        # return jsonify(response)


    return redirect(url_for('experiment.save_experiment',t_name=t_name,id=t_id))






@experimentBP.route('/show/<string:t_name>/<int:t_id>', methods=['GET'])
def show_experiment(t_name,t_id):
        experiments = Experiment.query.filter_by(T_id=t_id).all()
        return render_template('Experiment.html',title='Sample Verify',header='Sample Case',name=t_name,id=t_id,experiments=experiments)

@experimentBP.route('/submit/<string:t_name>/<int:t_id>/<int:e_id>', methods=['GET', 'POST'])
def submit_experiment(t_name,t_id,e_id):
        experiment = Experiment.query.filter_by(E_id=e_id).first()
        with db.auto_commit():
                new_request = Request(
                    T_id=t_id,
                    RequestID=None,
                    A_ID=None,
                    CourseCategory=None,
                    CourseName=None,
                    EID=experiment.E_id,
                    Explanation=experiment.comments,
                    ScoreUpdateCount=None,
                    Request_Type="ADD_EXPERIMENT",
                    Approve_Type='wait'
                )
                with db.auto_commit():
                    db.session.add(new_request)
        flash("Request submitted successfully")
        return redirect(url_for('experiment.show_experiment',t_name=t_name,t_id=t_id))


@experimentBP.route('/delete/<string:t_name>/<int:t_id>/<int:e_id>', methods=['GET', 'POST'])
def delete_experiment(t_name,t_id,e_id):
    experiment = Experiment.query.filter_by(E_id=e_id).first()
    with db.auto_commit():
        db.session.delete(experiment)
    flash("Delete successfully")
    return redirect(url_for('experiment.show_experiment', t_name=t_name, t_id=t_id))